#include <stdlib.h>
#include <stdio.h>
#include "../../../../mac/list.h"

#define LIST_INT_ERR -10

typedef struct int_list {
    struct int_list *next;
    int value;
} list_int_t;

LIST(list);

char list_int_init()
{
    return list_init(list);
}

char list_int_add_head(int value)
{
    list_int_t *v = malloc(sizeof(list_int_t));

    if (v == NULL) {
        return LIST_INT_ERR;
    }

    v->value = value;

    return list_add_head(list, v);
}

/*! if the list is empty, return a big int 4000000000 */
int list_int_get_head()
{
    list_int_t *v;

    v = list_get_head(list);
    if (v == NULL)
        return LIST_EMP;
    else
        return v->value;
}

/*! if the list is empty, return a big int 4000000000 */
int list_int_get_tail()
{   
    list_int_t *v;

    v = list_get_tail(list);
    if (v == NULL)
        return LIST_EMP;
    else
        return v->value;
}

char list_int_add_tail(int value)
{
    list_int_t *v;

    v = malloc(sizeof(list_int_t));
    if (v == NULL)
        return LIST_INT_ERR;

    v->value = value;
    return list_add_tail(list, v);
}

bool list_int_is_empty()
{
    return list_is_empty(list);
}

short list_int_get_length()
{
    return list_get_length(list);
}

char list_int_del(int value)
{
    list_int_t *v;
    list_int_t *p;
    int r;

    for (v = *list; v != NULL; v = v->next) {
        if (v->value == value) {
            break;
        }
    }

    if (v != NULL) {
        p = list_del(list, v);
        r = p->value;
        free(p);
        return r;
    }
    return LIST_ERR; 
}

int list_int_del_head()
{
    list_int_t *v;
    int value;

    v = list_del_head(list);
    if (v == NULL)
        return LIST_EMP;
    else {
        value = v->value;
        free(v);
    }
    return value;
}

/*! release the memory pointed by each items */
char list_int_release()
{
    list_int_t *v;
    list_int_t *p; /*!< pointer to the item to be released */

    if (*list == NULL)
        return LIST_EMP;

    v = *list;
    while (v != NULL) {
        p = v;
        v = v->next;
        free(p);
    }
    *list = NULL;

    return LIST_OK;
}



